package org.lex.checksum.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.SystemColor;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import org.lex.checksum.core.AlgorithmService;

public class Checksum extends Frame {
	private static final long serialVersionUID = 373605981183276605L;
	private Panel pane = null;
	private Label fileLabel = null;
	private TextField fileTextField = null;
	private Choice algorithmChoice = null;
	private ProgressBar progressBar = null;
	private TextArea textArea = null;
	private Label algorithmLabel = null;
	private Panel controlPane = null;
	private Button copyButton = null;
	private Button clearButton = null;

	public static void main(String[] args) {
		Checksum app = new Checksum();
		app.setLocationRelativeTo(null);
		app.setVisible(true);
	}

	public Checksum() {
		super();
		initialize();
	}

	public String getAlgorithm() {
		return this.getAlgorithmChoice().getSelectedItem();
	}

	public void setFile(File file) {
		this.getFileTextField().setText(file.getName());
	}

	public void setProgress(int progress) {
		this.getProgressBar().setProgress(progress);
	}

	public void addResult(String result, File file) {
		this.getTextArea().append(result + "\t" + file.getName() + "\n");
	}

	private void initialize() {
		this.setSize(400, 230);
		this.setTitle("Checksum");
		this.add(getPane(), BorderLayout.CENTER);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
		new DropHandler(this);
	}

	private Panel getPane() {
		if (pane == null) {
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 2;
			gridBagConstraints12.anchor = GridBagConstraints.EAST;
			gridBagConstraints12.insets = new Insets(3, 0, 5, 0);
			gridBagConstraints12.gridy = 0;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.insets = new Insets(3, 5, 5, 0);
			gridBagConstraints11.gridy = 0;
			algorithmLabel = new Label();
			algorithmLabel.setText("Type");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridy = 3;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.weighty = 1.0;
			gridBagConstraints4.gridwidth = 3;
			gridBagConstraints4.insets = new Insets(0, 5, 6, 6);
			gridBagConstraints4.gridx = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.insets = new Insets(0, 5, 5, 6);
			gridBagConstraints3.gridwidth = 3;
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.insets = new Insets(3, 5, 5, 6);
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.fill = GridBagConstraints.NONE;
			gridBagConstraints2.weightx = 0.0;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new Insets(0, 5, 5, 6);
			gridBagConstraints1.anchor = GridBagConstraints.CENTER;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(0, 5, 5, 0);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridy = 1;
			fileLabel = new Label();
			fileLabel.setText("File");
			pane = new Panel();
			pane.setBackground(SystemColor.control);
			pane.setLayout(new GridBagLayout());
			pane.add(fileLabel, gridBagConstraints);
			pane.add(getFileTextField(), gridBagConstraints1);
			pane.add(getAlgorithmChoice(), gridBagConstraints2);
			pane.add(getProgressBar(), gridBagConstraints3);
			pane.add(getTextArea(), gridBagConstraints4);
			pane.add(algorithmLabel, gridBagConstraints11);
			pane.add(getControlPane(), gridBagConstraints12);
		}
		return pane;
	}

	private TextField getFileTextField() {
		if (fileTextField == null) {
			fileTextField = new TextField();
			fileTextField.setEditable(false);
		}
		return fileTextField;
	}

	private Choice getAlgorithmChoice() {
		if (algorithmChoice == null) {
			algorithmChoice = new Choice();
			for (String algoName : AlgorithmService.getInstance().getAlogrithms()) {
				algorithmChoice.addItem(algoName);
			}
			algorithmChoice.select("MD5");
		}
		return algorithmChoice;
	}

	private ProgressBar getProgressBar() {
		if (null == this.progressBar) {
			progressBar = new ProgressBar();
			progressBar.setMinimumSize(new Dimension(100, 11));
		}
		return progressBar;
	}

	private TextArea getTextArea() {
		if (textArea == null) {
			textArea = new TextArea();
			textArea.setEditable(false);
		}
		return textArea;
	}

	private Panel getControlPane() {
		if (controlPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setVgap(0);
			flowLayout.setHgap(6);
			controlPane = new Panel();
			controlPane.setLayout(flowLayout);
			controlPane.add(getCopyButton(), null);
			controlPane.add(getClearButton(), null);
		}
		return controlPane;
	}

	private Button getCopyButton() {
		if (copyButton == null) {
			copyButton = new Button();
			copyButton.setLabel("Copy");
			copyButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String text = getTextArea().getText();
					Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), new ClipboardOwner() {
						public void lostOwnership(Clipboard c, Transferable t) {
						}
					});
				}
			});
		}
		return copyButton;
	}

	private Button getClearButton() {
		if (clearButton == null) {
			clearButton = new Button();
			clearButton.setLabel("Clear");
			clearButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getTextArea().setText("");
					setProgress(0);
					getFileTextField().setText("");
				}
			});
		}
		return clearButton;
	}
}
